package cn.stu.cms.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.stu.cms.dto.PageDto;
import cn.stu.cms.mapper.TeamsMapper;
import cn.stu.cms.pojo.Employees;
import cn.stu.cms.pojo.Teams;
import cn.stu.cms.service.EmployeesService;
import cn.stu.cms.service.TeamsService;
import cn.stu.cms.util.BeanUtils;
import cn.stu.cms.util.R;
import cn.stu.cms.vo.TeamsVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

import static cn.stu.cms.common.CommonConstants.*;

/**
 * 团队服务实施
 * (Teams)表服务实现类
 *
 * @author gh
 * @date 2023/10/09
 * @since 2023-07-21 17:11:33
 */
@Service
@AllArgsConstructor
public class TeamsServiceImpl extends ServiceImpl<TeamsMapper, Teams> implements TeamsService {

    private EmployeesService employeesService;

    /**
     * 添加
     *
     * @param teams 团队
     * @return {@link R}<{@link Boolean}>
     */
    @Override
    public R<Boolean> add(Teams teams) {
        baseMapper.insert(teams);
        return R.ok(SAVE_OK_MSG);
    }

    /**
     * 编辑
     *
     * @param teams 团队
     * @return {@link R}<{@link Boolean}>
     */
    @Override
    public R<Boolean> edit(Teams teams) {
        boolean exists = lambdaQuery().eq(Teams::getTeamId, teams.getTeamId()).exists();
        if (!exists) {
            return R.failed(ID_ERROR);
        }
        baseMapper.updateById(teams);
        return R.ok(EDIT_OK_MSG);
    }

    /**
     * 按ID删除
     *
     * @param id ID
     * @return {@link R}<{@link Boolean}>
     */
    @Override
    public R<Boolean> deleteById(Long id) {
        boolean exists = lambdaQuery().eq(Teams::getTeamId, id).exists();
        if (!exists) {
            return R.failed(ID_ERROR);
        }
        boolean employeeExists = employeesService.lambdaQuery().eq(Employees::getTeamId, id).exists();
        if (employeeExists) {
            return R.failed("团队不为空");
        }
        baseMapper.deleteById(id);
        return R.ok(DEL_OK_MSG);
    }

    /**
     * 获取列表
     *
     * @param pageDto 页数到页
     * @param teams   团队
     * @return {@link R}<{@link Page}<{@link Teams}>>
     */
    @Override
    public R<Page<Teams>> getList(PageDto<Teams> pageDto, Teams teams) {
        lambdaQuery().like(StrUtil.isNotBlank(teams.getTeamName()), Teams::getTeamName, teams.getTeamName())
                .page(pageDto);
        return R.ok(pageDto, GET_INFO_OK);
    }



    /**
     * 按ID获取团队
     *
     * @param id ID
     * @return {@link R}<{@link TeamsVo}>
     */
    @Override
    public R<TeamsVo> getTeamById(Long id) {
        boolean exists = lambdaQuery().eq(Teams::getTeamId, id).exists();
        if (!exists) {
            return R.failed(ID_ERROR);
        }
        Teams teams = baseMapper.selectById(id);
        TeamsVo teamsVo = BeanUtils.copier(teams, TeamsVo.class);
        List<Employees> employeeList = employeesService.lambdaQuery().eq(Employees::getTeamId, id).list();
        teamsVo.setEmployeesList(employeeList);
        return R.ok(teamsVo, GET_INFO_OK);
    }
}
